package com.jhmk.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.jhmk.springboot.exception.ParamsException;
import com.jhmk.springboot.model.ResultInfo;
import com.jhmk.springboot.query.UserQuery;
import com.jhmk.springboot.service.PatientVisitInfoService;
import com.jhmk.springboot.service.UserService;
import com.jhmk.springboot.vo.PatientVisitInfo;
import com.jhmk.springboot.vo.User;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "用户模块管理")
@RestController
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private PatientVisitInfoService patientVisitInfoService;

    private static Logger logger= LoggerFactory.getLogger(UserController.class);

    @ApiImplicitParam(name="id",value = "查询的id",required = true,paramType = "path")
    @ApiOperation(value = "用户模块-通过id查询患者实体")
    @GetMapping("user/{id}")
    public User queryUserByUserId(@PathVariable String id){
        logger.info("用户请求查询...");
        //logger.info("查询参数："+id);
        return userService.queryUserByUserId(id);
    }
    @ApiOperation(value = "用户模块-通过名字查询患者实体")
    @GetMapping("userByName")
    public ResultInfo queryUserByName(String name){
        logger.info("用户通过名字模糊查询...");
        logger.info("获取到的传入参数为："+name);
        ResultInfo info=new ResultInfo();
        try {
            List<User> users= userService.queryUserByUserName(name);
            info.setResult(users);
        }catch (Exception e){
            info.setCode(500);
            info.setResult(e.getMessage());
        }
        return info;
    }
    @ApiOperation(value = "用户模块-用户列表查询")
    @GetMapping("userPage")
    public PageInfo<User> queryUserByParams(UserQuery query){

        return userService.queryUserByParams(query);
    }
    @ApiOperation(value = "用户模块-用户添加")
    @PostMapping("addUser")
    public ResultInfo addUser(@RequestBody User user){
        ResultInfo info=new ResultInfo();
        try
        {
            int a = userService.addUser(user);
            info.setResult(a);
        }catch (ParamsException ex){
            info.setResult(ex.getMessage());
            info.setCode(500);
        } catch (Exception ex1){
            info.setResult("出现错误！");
            info.setCode(500);
        }
        return info;
    }
    @ApiOperation(value = "用户模块-用户更新")
    @PostMapping("updateUser")
    public ResultInfo updateUser(@RequestBody User user){
        ResultInfo info=new ResultInfo();
        int a= userService.updateUser(user);
        info.setResult(a);
        return info;
    }
    @ApiOperation(value = "用户模块-用户删除")
    @DeleteMapping("deleteUser")
    public ResultInfo deleteUser(@RequestParam("id") String id){
        ResultInfo info=new ResultInfo();
        int a= userService.deleteUser(id);
        info.setResult(a);
        return info;
    }



    @ApiOperation(value = "用户模块-其他")
    @GetMapping("listPaient")
    public ResultInfo listPaient(@RequestParam("id") String id){
       List<PatientVisitInfo> res= patientVisitInfoService.getListByDate(null);
        for (PatientVisitInfo item : res ){
            System.out.println(item);
        }
        return  new ResultInfo();
    }

    @PostMapping("addQUser")
    public ResultInfo addQUser(@Valid User user){
        ResultInfo info=new ResultInfo();
        int a = userService.addUser(user);
         info.setResult(a);
        return info;
    }
}
